﻿package com.mui.controls
{
    import __AS3__.vec.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.core.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;

    public class GTabBar2 extends GSprite
    {
        protected var _selectIndex:int = -1;
        protected var _direction:String;
        protected var _gap:Number = 0;
        protected var _cellWidth:Number = 0;
        protected var _cellHeight:Number = 0;
        protected var _dataProvider:Array;
        protected var vctabBar2Cell:Vector.<ITabBar2Cell>;
        protected var _cellRenderer:Class;
        public var compelDispatchEventSelectedChange:Boolean;

        public function GTabBar2()
        {
            this.vctabBar2Cell = new Vector.<ITabBar2Cell>;
            this.tabChildren = false;
            return;
        }// end function

        public function itemToCell(param1) : ITabBar2Cell
        {
            var _loc_2:int = 0;
            if (this._dataProvider)
            {
                _loc_2 = 0;
                while (_loc_2 < this._dataProvider.length)
                {
                    
                    if (this._dataProvider[_loc_2] == param1)
                    {
                        return this.vctabBar2Cell[_loc_2];
                    }
                    _loc_2++;
                }
            }
            return null;
        }// end function

        public function get dataProvider() : Array
        {
            return this._dataProvider;
        }// end function

        public function set dataProvider(param1:Array) : void
        {
            var _loc_3:int = 0;
            this._dataProvider = param1;
            var _loc_2:* = this.vctabBar2Cell.length;
            _loc_2 = param1.length > _loc_2 ? (_loc_2) : (param1.length);
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                this.vctabBar2Cell[_loc_3].data = param1[_loc_3];
                _loc_3++;
            }
            _loc_3 = _loc_2;
            while (_loc_3 < this.vctabBar2Cell.length)
            {
                
                this.vctabBar2Cell[_loc_3].dispose();
                _loc_3++;
            }
            this.vctabBar2Cell.length = _loc_2;
            _loc_3 = _loc_2;
            while (_loc_3 < param1.length)
            {
                
                this.vctabBar2Cell[_loc_3] = UICompomentPool.getUICompoment(this._cellRenderer);
                if (this.vctabBar2Cell[_loc_3] is IFrUIContainer)
                {
                    (this.vctabBar2Cell[_loc_3] as IFrUIContainer).createDisposedChildren();
                }
                this.vctabBar2Cell[_loc_3].data = param1[_loc_3];
                (this.vctabBar2Cell[_loc_3] as IFrUI).configEventListener(MouseEvent.CLICK, this.onClickTabBarCell);
                (this.vctabBar2Cell[_loc_3] as IFrUI).configEventListener(MouseEvent.ROLL_OVER, this.onRollOverTabBarCell);
                (this.vctabBar2Cell[_loc_3] as IFrUI).configEventListener(MouseEvent.ROLL_OUT, this.onRollOutTabBarCell);
                this.addChild(this.vctabBar2Cell[_loc_3] as DisplayObject);
                _loc_3++;
            }
            if (param1.length > 0)
            {
                this.selectIndex = 0;
            }
            else
            {
                this.selectIndex = -1;
            }
            this.updatePos();
            return;
        }// end function

        protected function updatePos() : void
        {
            var _loc_1:int = 0;
            if (this._direction == GBoxDirection.HORIZONTAL)
            {
                _loc_1 = 0;
                while (_loc_1 < this.vctabBar2Cell.length)
                {
                    
                    (this.vctabBar2Cell[_loc_1] as Sprite).x = (this._cellWidth + this._gap) * _loc_1;
                    _loc_1++;
                }
            }
            else
            {
                _loc_1 = 0;
                while (_loc_1 < this.vctabBar2Cell.length)
                {
                    
                    (this.vctabBar2Cell[_loc_1] as Sprite).y = (this._cellHeight + this._gap) * _loc_1;
                    _loc_1++;
                }
            }
            return;
        }// end function

        protected function onClickTabBarCell(event:MouseEvent) : void
        {
            this.selectIndex = this.getChildIndex(event.currentTarget as DisplayObject);
            return;
        }// end function

        protected function onRollOverTabBarCell(event:MouseEvent) : void
        {
            if (this.selectIndex != this.getChildIndex(event.currentTarget as DisplayObject))
            {
                (event.currentTarget as ITabBar2Cell).over();
            }
            return;
        }// end function

        protected function onRollOutTabBarCell(event:MouseEvent) : void
        {
            if (this.selectIndex != this.getChildIndex(event.currentTarget as DisplayObject))
            {
                (event.currentTarget as ITabBar2Cell).out();
            }
            return;
        }// end function

        public function set selectIndex(param1:int) : void
        {
            if (param1 == this._selectIndex && this.compelDispatchEventSelectedChange == false)
            {
                return;
            }
            if (this._selectIndex >= 0)
            {
                (this.getChildAt(this._selectIndex) as ITabBar2Cell).selected = false;
            }
            this._selectIndex = param1;
            if (this._selectIndex >= 0)
            {
                (this.getChildAt(this._selectIndex) as ITabBar2Cell).selected = true;
            }
            dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, this._selectIndex));
            return;
        }// end function

        public function get selectIndex() : int
        {
            return this._selectIndex;
        }// end function

        public function get direction() : String
        {
            return this._direction;
        }// end function

        public function set direction(param1:String) : void
        {
            if (this._direction == param1)
            {
                return;
            }
            this._direction = param1;
            return;
        }// end function

        public function set gap(param1:Number) : void
        {
            this._gap = param1;
            return;
        }// end function

        public function set cellWidth(param1:Number) : void
        {
            this._cellWidth = param1;
            return;
        }// end function

        public function set cellHeight(param1:Number) : void
        {
            this._cellHeight = param1;
            return;
        }// end function

        public function set cellRenderer(param1:Class) : void
        {
            this._cellRenderer = param1;
            return;
        }// end function

        public function get selectedItem() : Object
        {
            if (this._dataProvider && this._dataProvider.length > this._selectIndex)
            {
                return this._dataProvider[this._selectIndex];
            }
            return null;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            this.compelDispatchEventSelectedChange = false;
            var _loc_2:* = this.vctabBar2Cell.length - 1;
            while (_loc_2 >= 0)
            {
                
                this.vctabBar2Cell[_loc_2].dispose();
                (this.vctabBar2Cell[_loc_2] as Sprite).removeEventListener(MouseEvent.CLICK, this.onClickTabBarCell);
                (this.vctabBar2Cell[_loc_2] as Sprite).removeEventListener(MouseEvent.ROLL_OVER, this.onRollOverTabBarCell);
                (this.vctabBar2Cell[_loc_2] as Sprite).removeEventListener(MouseEvent.ROLL_OUT, this.onRollOutTabBarCell);
                _loc_2 = _loc_2 - 1;
            }
            this.vctabBar2Cell.length = 0;
            this._selectIndex = -1;
            this._gap = 0;
            this._cellWidth = 0;
            this._cellHeight = 0;
            this._dataProvider = null;
            this._cellRenderer = null;
            return;
        }// end function

    }
}
